package com.jied.duodian.service.Impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.jied.duodian.dao.DdOrderDAO;
import com.jied.duodian.entity.DdOrder;
import com.jied.duodian.entity.DdOrderExample;
import com.jied.duodian.entity.DdProduct;
import com.jied.duodian.entity.DdShopping;
import com.jied.duodian.service.DdOrderService;
import com.jied.duodian.service.DdProductService;
import com.jied.duodian.service.DdShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author LiJie
 * @date Created on 2020/10/28
 * WWW https://github.com/j6l
 */
@Service
@Transactional
public class DdOrderServiceImpl implements DdOrderService {

    @Autowired
    DdOrderDAO ddOrderDAO;
    @Autowired
    DdProductService ddProduct;
    @Autowired
    DdShoppingService ddShoppingService;


    @Override
    public DdOrder getOrderByid(Integer id) {
        return ddOrderDAO.selectByPrimaryKey( id );
    }

    @Override
    public DdOrder getOrderBysu(String id) {
        DdOrderExample example = new DdOrderExample();
        DdOrderExample.Criteria criteria = example.createCriteria();
        criteria.andOrderSnEqualTo( id );
        return ddOrderDAO.selectByExample( example ).get( 0 );
    }

    @Override
    public Integer addOrderList(DdOrder ddOrder) {
        return ddOrderDAO.insertSelective( ddOrder );
    }

    @Override
    public void getsdasd(DdShopping shopping, DdOrder ddOrder, DdProduct product) {
        ddProduct.updataProduct( product );
        addOrderList( ddOrder );
        ddShoppingService.delshoppding( shopping );
    }

    @Override
    public List<DdOrder> getallbyusername(String getuserName) {
        DdOrderExample example = new DdOrderExample();
        DdOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo( getuserName );
        return ddOrderDAO.selectByExample( example );
    }

    @Override
    public List<DdOrder> getOrder() {
        DdOrderExample example = new DdOrderExample();
        DdOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        return ddOrderDAO.selectByExample( example );
    }

    @Override
    public Integer updataOrderByid(DdOrder order) {
        return ddOrderDAO.updateByPrimaryKeySelective( order );
    }

    @Override
    public Integer delorderbyid(Integer id) {
        DdOrder orderByid = getOrderByid( id );
        DdProduct product = ddProduct.getProductByid( orderByid.getProductId() );
        product.setStock( product.getStock() + orderByid.getProductNum() );
        product.setActivity( product.getActivity() - orderByid.getProductNum() );
        ddProduct.updataProduct( product );
        return ddOrderDAO.deleteByPrimaryKey( id );
    }

    @Override
    @Cacheable(cacheNames = "qt", keyGenerator = "keyGenerator")
    public long getmonth(LocalDate firstday) {
        LocalDate plusDays = firstday.plusDays( 1l );
        long count = getCount( firstday, plusDays );
        return count;
    }

    private long getCount(LocalDate firstday, LocalDate plusDays) {
        ZoneId zone = ZoneId.systemDefault();
        DdOrderExample example = new DdOrderExample();
        DdOrderExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeGreaterThanOrEqualTo(
                Date.from( firstday.atStartOfDay().atZone( zone ).toInstant() ) );
        criteria.andCreateTimeLessThanOrEqualTo(
                Date.from( plusDays.atStartOfDay().atZone( zone ).toInstant() ) );
        return ddOrderDAO.countByExample( example );
    }

    @Override
    public Long getOrderzt() {
        final LocalDate plusDays = LocalDate.now().plusDays( 1 );
        final LocalDate minusDays = LocalDate.now().minusDays( 1l );
        long count = getCount( plusDays, minusDays );
        return count;
    }

    @Override
    public Long getOrdernopay() {
        DdOrderExample example = new DdOrderExample();
        DdOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIspayEqualTo( 0 );
        long count = ddOrderDAO.countByExample( example );
        return count;
    }

    @Override
    public Long getOrdernofa() {
        DdOrderExample example = new DdOrderExample();
        DdOrderExample.Criteria criteria = example.createCriteria();
        criteria.andFaAddressIsNull();
        long count = ddOrderDAO.countByExample( example );
        return count;
    }
}
